<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');

	class m_user extends CI_Model
	{
		var $userId = null;
		var $userEmail = null;
		var $userPassword = null;
		var $userStatus = null;

		function __construct()
    	{
    	    // Call the Model constructor
    	    parent::__construct();
    	}

		public function get_user_id()
		{
			$this->load->database();
			$query = $this->db->get_where('tblUser', array('userEmail' => $this->userEmail));
			if (sizeof($query->result()))
			{
				$this->userId = $query->row()->userId;
				$this->userStatus = $query->row()->userStatus;
			}
			else
			{
				$this->userId = 0;
			}
			return $this->userId;
		}

	   	public function set_user_email($email)
	   	{
	   		$this->userEmail = "$email";
	   	}

		public function set_user_password($password)
  		{
   			$this->userPassword = "$password";
   		}

   		public function add_user()
    	{
    		// Check whether the userEmail already exists in the database
			if($this->get_user_id())
			{
				return -1;
			}
			else
			{
				$data = array(
    				'userEmail' => "$this->userEmail",
    				'userPassword' => "$this->userPassword"
    			);
				$this->db->insert('tblUser', $data);
				return 0;
			}
    	}

    	public function reset_password()
    	{
    		$this->get_user_id();
    		$this->db->where('userId', $this->userId);
    		$this->db->update('tblUser', $this);

    		return 0;
    	}

    	public function validate_user()
    	{
    		if($this->get_user_id())
    		{
    			if($this->userStatus == 0)
    			{
    				$this->userStatus = 1;
    				$this->db->where('userId', $this->userId);
    				$this->db->update('tblUser', $this);

    				return 0;
    			}
    			else
    			{
    				// User already validated
    				return -2000;
    			}
    		}
    		else
    		{
    			// No such user
    			return -1000;
    		}
    	}
	}
?>
